/*   1:    */ package com.huawei.m2m.cig.decoder.hzhiqiang.wzhiqiang.generate.adaptor;
/*   2:    */ 
/*   3:    */ import com.fasterxml.jackson.databind.JsonNode;
/*   4:    */ import com.fasterxml.jackson.databind.node.ObjectNode;
/*   5:    */ import com.huawei.m2m.cig.decoder.hzhiqiang.wzhiqiang.generate.factory.DefaultCloudMessageFactory;
/*   6:    */ import com.huawei.m2m.cig.decoder.hzhiqiang.wzhiqiang.generate.factory.DefaultMessageFactory;
/*   7:    */ import com.huawei.m2m.cig.decoder.protocol.BigEndianInputBuffer;
/*   8:    */ import com.huawei.m2m.cig.decoder.protocol.BigEndianOutputBuffer;
/*   9:    */ import com.huawei.m2m.cig.decoder.protocol.InputBuffer;
/*  10:    */ import com.huawei.m2m.cig.decoder.protocol.NBMessage;
/*  11:    */ import com.huawei.m2m.cig.decoder.protocol.OutputBuffer;
/*  12:    */ import com.huawei.m2m.cig.tup.modules.protocol_adapter.IProtocolAdapter;
/*  13:    */ import java.util.Arrays;
/*  14:    */ import org.osgi.framework.BundleContext;
/*  15:    */ import org.slf4j.Logger;
/*  16:    */ import org.slf4j.LoggerFactory;
/*  17:    */ 
/*  18:    */ public class ProtocolAdapterImpl
/*  19:    */   implements IProtocolAdapter
/*  20:    */ {
/*  21: 22 */   private static final Logger LOGGER = LoggerFactory.getLogger(ProtocolAdapterImpl.class);
/*  22:    */   private static final String MANU_FACTURERID = "zhiqiang";
/*  23:    */   private static final String MODEL = "zhiqiang";
/*  24:    */   
/*  25:    */   public void activate(BundleContext paramBundleContext)
/*  26:    */   {
/*  27: 29 */     LOGGER.info("ProtocolAdapterImpl activate");
/*  28:    */   }
/*  29:    */   
/*  30:    */   public void deactivate(BundleContext paramBundleContext)
/*  31:    */   {
/*  32: 33 */     LOGGER.info("ProtocolAdapterImpl deactivate");
/*  33:    */   }
/*  34:    */   
/*  35:    */   public ObjectNode decode(byte[] paramArrayOfByte)
/*  36:    */     throws Exception
/*  37:    */   {
/*  38: 40 */     if (paramArrayOfByte == null) {
/*  39: 41 */       throw new NullPointerException("rawData is null");
/*  40:    */     }
/*  41: 44 */     LOGGER.info("receive raw message {}", Arrays.toString(paramArrayOfByte));
/*  42:    */     
/*  43: 46 */     int i = getMessageId(paramArrayOfByte);
/*  44: 47 */     NBMessage localNBMessage = DefaultMessageFactory.getInstance().createMessage(i);
/*  45: 48 */     if (localNBMessage == null)
/*  46:    */     {
/*  47: 49 */       LOGGER.error("msg  id={} not found!", Integer.valueOf(i));
/*  48: 50 */       return null;
/*  49:    */     }
/*  50: 53 */     BigEndianInputBuffer localBigEndianInputBuffer = new BigEndianInputBuffer(paramArrayOfByte);
/*  51: 54 */     localNBMessage.decode(localBigEndianInputBuffer);
/*  52: 55 */     LOGGER.info("decode jsonNode is {}", localNBMessage.toJsonNode());
/*  53: 56 */     return localNBMessage.toJsonNode();
/*  54:    */   }
/*  55:    */   
/*  56:    */   private int getMessageId(byte[] paramArrayOfByte)
/*  57:    */   {
/*  58: 60 */     byte[] arrayOfByte = new byte[1];
/*  59: 61 */     System.arraycopy(paramArrayOfByte, 0, arrayOfByte, 0, 1);
/*  60: 62 */     BigEndianInputBuffer localBigEndianInputBuffer = new BigEndianInputBuffer(arrayOfByte);
/*  61: 63 */     return localBigEndianInputBuffer.getByte();
/*  62:    */   }
/*  63:    */   
/*  64:    */   public byte[] encode(ObjectNode paramObjectNode)
/*  65:    */     throws Exception
/*  66:    */   {
/*  67: 70 */     if (paramObjectNode == null)
/*  68:    */     {
/*  69: 71 */       LOGGER.error("objectNode == null");
/*  70: 72 */       return null;
/*  71:    */     }
/*  72: 75 */     LOGGER.info("encode jsonNode is {}", paramObjectNode);
/*  73: 76 */     if (!paramObjectNode.has("msgType"))
/*  74:    */     {
/*  75: 77 */       LOGGER.error("msgType not found");
/*  76: 78 */       return null;
/*  77:    */     }
/*  78: 81 */     String str = paramObjectNode.get("msgType").asText();
/*  79: 82 */     if ("cloudReq".equals(str)) {
/*  80: 83 */       return encodeCloudRequest(paramObjectNode);
/*  81:    */     }
/*  82: 84 */     if ("cloudRsp".equals(str)) {
/*  83: 85 */       return encodeCloudResponse(paramObjectNode);
/*  84:    */     }
/*  85: 87 */     LOGGER.error(String.format("invalid msgType {}", new Object[] { str }));
/*  86: 88 */     return null;
/*  87:    */   }
/*  88:    */   
/*  89:    */   private byte[] encodeCloudRequest(ObjectNode paramObjectNode)
/*  90:    */     throws Exception
/*  91:    */   {
/*  92: 96 */     if (!paramObjectNode.has("cmd"))
/*  93:    */     {
/*  94: 97 */       LOGGER.error("cmd not found, objectNode is {}", paramObjectNode);
/*  95: 98 */       return null;
/*  96:    */     }
/*  97:101 */     String str = paramObjectNode.get("cmd").asText();
/*  98:    */     
/*  99:103 */     return encodeHelper(paramObjectNode, str);
/* 100:    */   }
/* 101:    */   
/* 102:    */   private byte[] encodeCloudResponse(ObjectNode paramObjectNode)
/* 103:    */     throws Exception
/* 104:    */   {
/* 105:110 */     String str = DefaultCloudMessageFactory.getInstance().getCloudMessageRspName();
/* 106:112 */     if (isStringEmpty(str)) {
/* 107:113 */       return null;
/* 108:    */     }
/* 109:116 */     byte[] arrayOfByte = new byte[4];
/* 110:117 */     arrayOfByte[0] = -86;
/* 111:118 */     arrayOfByte[1] = -86;
/* 112:119 */     arrayOfByte[2] = 0;
/* 113:120 */     arrayOfByte[3] = 0;
/* 114:121 */     return arrayOfByte;
/* 115:    */   }
/* 116:    */   
/* 117:    */   private byte[] encodeHelper(ObjectNode paramObjectNode, String paramString)
/* 118:    */     throws Exception
/* 119:    */   {
/* 120:130 */     if (isStringEmpty(paramString)) {
/* 121:131 */       return null;
/* 122:    */     }
/* 123:134 */     NBMessage localNBMessage = DefaultCloudMessageFactory.getInstance().createMessage(paramString);
/* 124:135 */     if (localNBMessage == null) {
/* 125:136 */       return null;
/* 126:    */     }
/* 127:139 */     localNBMessage.configJsonNode(paramObjectNode);
/* 128:    */     
/* 129:141 */     BigEndianOutputBuffer localBigEndianOutputBuffer = new BigEndianOutputBuffer();
/* 130:142 */     localNBMessage.encode(localBigEndianOutputBuffer);
/* 131:    */     
/* 132:144 */     LOGGER.info("result byte is {}", Arrays.toString(localBigEndianOutputBuffer.toByteArray()));
/* 133:    */     
/* 134:146 */     return localBigEndianOutputBuffer.toByteArray();
/* 135:    */   }
/* 136:    */   
/* 137:    */   public String getManufacturerId()
/* 138:    */   {
/* 139:153 */     return "zhiqiang";
/* 140:    */   }
/* 141:    */   
/* 142:    */   public String getModel()
/* 143:    */   {
/* 144:160 */     return "zhiqiang";
/* 145:    */   }
/* 146:    */   
/* 147:    */   private boolean isStringEmpty(String paramString)
/* 148:    */   {
/* 149:164 */     if ((paramString == null) || (paramString.isEmpty())) {
/* 150:165 */       return true;
/* 151:    */     }
/* 152:168 */     return false;
/* 153:    */   }
/* 154:    */ }


/* Location:           C:\Users\Zou Songhua\Downloads\zhiqiang_CIGPlug-in_sign\zhiqiang_CIGPlug-in.zip_sign\zhiqiang_CIGPlug-in\preload\zhiqiang-zhiqiang-zhiqiang-1.0.0.jar
 * Qualified Name:     com.huawei.m2m.cig.decoder.hzhiqiang.wzhiqiang.generate.adaptor.ProtocolAdapterImpl
 * JD-Core Version:    0.7.0.1
 */